//
//  ViewController.m
//  AFN001
//
//  Created by song on 15/8/16.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AFNetworkReachabilityManager *mgr =[AFNetworkReachabilityManager sharedManager];
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable:  //没有网络
                NSLog(@"没有网络");
                break;

           
            case AFNetworkReachabilityStatusReachableViaWWAN: //手机自带网络
                NSLog(@"手机自带网络");
                break;
                
            case  AFNetworkReachabilityStatusReachableViaWiFi: // 手机wifi
                NSLog(@"手机wifi");
                break;
        }
        
    }];
    [mgr startMonitoring];
    
    
    

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self AFNupload];
}

// 普通的get 请求，POST 请求也是这样子的
-(void)AFNget
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"name"]=@"song";
    params[@"passwd"]=@"1233";
    [mgr GET:@"http://localhost/IOStest/loginget.php"   parameters:params
     success:^(AFHTTPRequestOperation *operation , id responseObject) {
        
         NSLog(@"request  success --%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"request error %@", error);
    }];
    
}

// AFN 的文件上传
-(void)AFNupload
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    [mgr POST:@"http://localhost/IOStest/upload.php" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  formdata) {
        
        NSData *data = [NSData dataWithContentsOfFile:@"/Users/song/Desktop/data/tgs.txt"];
        [formdata appendPartWithFileData:data name:@"file" fileName:@"tgs.txt" mimeType:@"text/plain"];
        
    } success:^(AFHTTPRequestOperation * operation, id data) {
        NSLog(@"上传成功------%@",data);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        NSLog(@"上传失败  -----%@" ,error);
    }];
}


-(void)AFN
{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
